<?php


include('db.inc.php');

function likecate($path=''){
	$sql = "select id,catename,path,concat(path,',',id) as fullpath from likecate order by fullpath asc";
	$res = mysql_query($sql);
	$result = array();
	while($row = mysql_fetch_assoc($res)){
		$deep = count(explode(',', trim($row['fullpath'])));
		$row['catename'] = str_repeat('&nbsp;&nbsp;', $deep*4).'|--'.$row['catename'];
		$result[]=$row;
	}
	return $result;
}

// $res = likecate();
// print_r($res);


function displayLikecate(){
	$res = likecate();
	$str = '';
	$str .= "<select name=cate>";
	foreach ($res as $key => $value) {
		$str .= "<option>{$value['catename']}</option>";
	}
	$str .="</select>";
	return $str;
}

//echo displayLikecate();

function getPathCate($cateid){
	$sql = "select *,concat(path,',',id) fullpath from likecate where id =$cateid";
	$res = mysql_query($sql);
	$row = mysql_fetch_assoc($res);
	$ids = $row['fullpath'];
	$sql = "select * from likecate where id in ($ids) order by id asc";
	$res = mysql_query($sql);
	$result = array();
	while ($row = mysql_fetch_assoc($res)){
		$result[] = $row;
	}
	return $result;
}

// $res = getPathCate(2);
// print_r($res);

function displayCatePath($cateid,$link='likecate.php?cid='){



	$res = getPathCate($cateid);
	$str = '';
	foreach ($res as $key => $value) {
		$str .= "<a href='{$link}{$value['id']}'>{$value['catename']}</a>".'>';

	}
	return $str;
}

echo displayCatePath(2);